const express = require("express");
var router = express.Router()
const FocusModel = require("../../model/FocusModel")
const ManagerModel = require("../../model/ManagerModel");
const NavModel = require("../../model/NavModel");
const ArticleCateModel = require("../../model/ArticleCateModel")
const ArticleModel = require("../../model/ArticleModel")
const mongoose = require("../../model/core")


let appModel = {
    FocusModel,
    ManagerModel,
    NavModel,
    ArticleCateModel,
    ArticleModel
}


router.get("/", (req, res) => {
    res.render("admin/main/index.html")
})
router.get("/welcome", (req, res) => {
    res.send("欢迎来到后台管理中心")
})

router.get("/chStatus", async (req, res) => {
    let json

    let { id, model, field } = req.query
    model = model + 'Model'

    let modelResult = await appModel[model].find({ "_id": id })


    if (modelResult.length > 0) {
        let tempField = modelResult[0][field]

        tempField == 1 ? json = { [field]: 0 } : json = { [field]: 1 }
        await appModel[model].updateOne({ "_id": id }, json)

        // 修改成功返回success回客户端
        res.send({
            success: true,
            message: '修改状态成功'
        })
    } else {
        res.send({
            success: false,
            message: '传入参数错误'
        })
    }
})

router.get('/chSort', async function (req, res) {
    let json
    let { id, model, field, inputSort } = req.query
    model = model + 'Model'
    json = { [field]: inputSort }

    let modelResult = await appModel[model].find({ "_id": id })
    if (modelResult.length) {
        await appModel[model].updateOne({ "_id": id }, json)
        res.send({
            success: true,
            message: '修改成功'
        })
    } else {
        res.send({
            success: false,
            message: '传入参数错误'
        })
    }
})

module.exports = router